#include <stdio.h>	/* fprintf */
#include <stdlib.h>	/* contains exit */
#include <sys/types.h>	/* unistd.h needs this */
#include <unistd.h>	/* contains read/write */
#include <fcntl.h>
#include <malloc.h>
#include <string.h>
#include </usr/include/elf.h>
/*
typedef struct{
	unsigned char e_ident[EI_NIDENT];
	Elf32_Half e_type;
	Elf32_Half e_machine;
	Elf32_Word e_version;
	Elf32_Addr e_entry;
        Elf32_Off  e_phoff;
        Elf32_Off  e_shoff;
        Elf32_Word e_flags;
        Elf32_Half e_ehsize;
        Elf32_Half e_phentsize;
        Elf32_Half e_phnum;
        Elf32_Half e_shentsize;
        Elf32_Half e_shnum;
        Elf32_Half e_shstrndx;
}Elf32_Ehdr;
typedef struct
{
	Elf32_Word sh_name;  
	Elf32_Word sh_type; 
	Elf32_Word sh_flags; 
	Elf32_Addr sh_addr; 
	Elf32_Off sh_offset; 
	Elf32_Word sh_size; 
	Elf32_Word sh_link; 
	Elf32_Word sh_info; 
	Elf32_Word sh_addralign; 
	Elf32_Word sh_entsize; 
} Elf32_Shdr;
*/

struct exec {
  unsigned long a_magic;        /* Use macros N_MAGIC, etc for access */
  unsigned a_text;              /* length of text, in bytes */
  unsigned a_data;              /* length of data, in bytes */
  unsigned a_bss;               /* length of uninitialized data area for file, in bytes */
  unsigned a_syms;              /* length of symbol table data in file, in bytes */     
  unsigned a_entry;             /* start address */
  unsigned a_trsize;            /* length of relocation info for text, in bytes */
  unsigned a_drsize;            /* length of relocation info for data, in bytes */      
};   
#define ZMAGIC 0413


#define MINIX_HEADER 32
#define GCC_HEADER 4096

void die(char * str)
{
	fprintf(stderr,"%s\n",str);
	exit(1);
}

void usage(void)
{
	die("Usage: build input [> output]");
}

int main(int argc, char ** argv)
{
	int i,c,id;
	char buf[4096];
	
	Elf32_Ehdr ElfHead;
	Elf32_Ehdr * pElfHead = &ElfHead;

	int lenShdr;
	Elf32_Shdr * pShdr;

	char * pNameTab=0;
	char * pName;
	
	struct exec ex;
	unsigned long size_text, size_data, addr_text, addr_data;
	char * pWritebuf;

	if (argc != 2)
		usage();
	for (i=0;i<sizeof buf; i++) buf[i]=0;
	if ((id=open(argv[1],O_RDONLY,0))<0)
		die("Unable to open input");
	if (read(id,buf,GCC_HEADER) != GCC_HEADER)
		die("Unable to read input header");
	if (((long *) buf)[6] != 0)
		die("Non-GCC header of input");
	
	ElfHead = *((Elf32_Ehdr * )buf);
	fprintf(stderr, "%s", "ELF Header:\n");
	fprintf(stderr, "%s","Magic:");
	for(i=0; i<EI_NIDENT; i++)
	{
		fprintf(stderr, " 0x%x", pElfHead->e_ident[i]);
	}
	fprintf(stderr, "%s", "\n");
	fprintf(stderr, "Entry Point:\t0x%x\n", pElfHead->e_entry);
	fprintf(stderr, "S Header offet:\t0x%x\n", pElfHead->e_shoff);	
	fprintf(stderr, "S Header size:\t0x%x\n", pElfHead->e_shentsize);
	fprintf(stderr, "S Header num:\t0x%x\n", pElfHead->e_shnum);
	fprintf(stderr, "S Index:\t0x%x\n", pElfHead->e_shstrndx);

	lenShdr = pElfHead->e_shnum * pElfHead->e_shentsize;
	pShdr = (Elf32_Shdr *)malloc(lenShdr);
	if(lseek(id,pElfHead->e_shoff,SEEK_SET) >= 0)
	{
		c = read(id, pShdr, lenShdr);
	}
	else
	{
		die("Read Sesstion header failed!\n");
	}

	for(i=0; i<pElfHead->e_shnum; i++)
        {
		if(((pShdr[i]).sh_type) == SHT_STRTAB)
		{
			pNameTab = (char *)malloc((pShdr[i]).sh_size);
			if(lseek(id,(pShdr[i]).sh_offset,SEEK_SET) >= 0)
		 	{
				c = read(id, pNameTab, (pShdr[i]).sh_size);
			}
			else
			{
				die("Read Sesstion header failed!\n");
			}
			break;
		}
        }

	if(pNameTab == 0)
	{
		die("No SHT_STRTAB found!\n");
	}

	ex.a_magic = ZMAGIC;
	ex.a_trsize = 0;
	ex.a_drsize = 0;
	ex.a_syms = 0;
	ex.a_entry = 0;

	for(i=0; i<pElfHead->e_shnum; i++)
        {
                pName = &(pNameTab[(pShdr[i]).sh_name]);
                fprintf(stderr,"[%d]",i);
                fprintf(stderr,"\t0x%x",(pShdr[i]).sh_type);
                fprintf(stderr,"\t0x%x",(pShdr[i]).sh_offset);
                fprintf(stderr,"\t0x%x",(pShdr[i]).sh_addr);
                fprintf(stderr,"\t0x%x",(pShdr[i]).sh_size);
                fprintf(stderr,"\t%s",pName);
                fprintf(stderr,"%s","\n");

                if(strcmp(pName, ".text")==0)
                {
                        addr_text = (pShdr[i]).sh_offset;
			size_text = (pShdr[i]).sh_size;
			ex.a_text = (size_text + 0xfff) & 0xfffff000;
                        fprintf(stderr,"text addr 0x%x.\n",addr_text);
                        fprintf(stderr,"text size %d.\n",ex.a_text);
		}
		else if(strcmp(pName, ".rodata") == 0)
		{
                        addr_data = (pShdr[i]).sh_offset;
 			size_data = (pShdr[i]).sh_size;			
  			ex.a_data = (size_data + 0xfff) & 0xfffff000;
                        fprintf(stderr,"data addr 0x%x.\n",addr_data);
                        fprintf(stderr,"data size %d.\n",ex.a_data);
		}
		else if(strcmp(pName, ".bss")==0)
		{
  			ex.a_bss = ((pShdr[i]).sh_size + 0xfff) & 0xfffff000;
                        fprintf(stderr,"bss size %d.\n",ex.a_bss);
		}
	}
  
	write(1,(char *)(&ex),1024);
                        
	pWritebuf = (char *)malloc(ex.a_text);
	if(lseek(id,addr_text,SEEK_SET) >= 0)
	{
		if((c=read(id, pWritebuf, size_text)) > 0)
		{
			c = write(1,pWritebuf, ex.a_text);
			fprintf(stderr,"Write text %d bytes.\n",size_text);
		}
	}
	else
	{                               
	       	die("Seek Text Sesstion failed!\n");
	}

        pWritebuf = (char *)realloc(pWritebuf, ex.a_data);
        if(lseek(id,addr_data,SEEK_SET) >= 0)
        {
                if((c=read(id, pWritebuf, size_data)) > 0)
                {
                        c = write(1,pWritebuf, ex.a_data);
                        fprintf(stderr,"Write data  %d bytes.\n",size_data);
                }
        }
        else
        {
                die("Seek Data Sesstion failed!\n");
        }
	
	close(id);
	close(1);
	return(0);
}
